//
//  LeftMuneViewController.m
//  Kaiser
//
//  Created by wei wang on 2016/12/31.
//  Copyright © 2016年 wei wang. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "AccountInfoController.h"
#import "KaiseruserDataGit.h"
#import "MsgNotificationViewController.h"
#import "SuggestionViewController.h"
#import "HelpViewController.h"
#import "ChoosePatternViewController.h"
@interface LeftMenuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIImageView *userImage;

@property (weak, nonatomic) IBOutlet UILabel *userName;

/** indexMeassge **/
@property(nonatomic,strong) NSString *indexMeassge;
@end


@implementation LeftMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UITableView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.indexMeassge = @"0";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:@"count" object:nil];
}

- (void)updateTableView{
    
    
    
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    //还要指定存储文件的文件名称,仍然使用字符串拼接
    NSString *filePath = [docPath stringByAppendingPathComponent:@"MeasgeHistoryCount.plist"];
    NSLog(@"%@",filePath);
    NSArray *readArray = [NSArray arrayWithContentsOfFile:filePath];
    
    if (readArray.count > 0) {
        
        self.indexMeassge = [NSString stringWithFormat:@"%lu",(unsigned long)readArray.count];
        
    }else{
        
        self.indexMeassge = @"0";
        
    }
    [self.tableView reloadData];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    UserInfo *userInfo = [KaiseruserDataGit loadUserInfo];
    self.userName.text = userInfo.nickname;
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:userInfo.image] placeholderImage:[UIImage imageNamed:@"bg_head_portrait"]];

    NSLog(@"%@",userInfo);
}


#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"leftViewCell0_%ld",(long)indexPath.row]];
#pragma mark - view.tag = 10  lable.tag = 11;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView *placeholderView = (UIView *)[cell viewWithTag:10];
    UILabel *placeholderLabel = (UILabel *)[cell viewWithTag:11];
    
    if ([self.indexMeassge isEqualToString:@"0"]) {
        placeholderView.hidden = YES;
        
    }else{
        
        placeholderView.hidden = NO;
        placeholderLabel.text = self.indexMeassge;
    }
    
    
    
    
    return cell;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    switch (indexPath.row) {
        case 0:
        {
            AccountInfoController *vc = [AccountInfoController new];
            vc.indextag = 0;
            [self presentViewController:[[UINavigationController alloc]initWithRootViewController:[[AccountInfoController alloc]init]] animated:YES completion:nil];
            
            break;
        }
        case 1:
        {
            NSFileManager *manager=[NSFileManager defaultManager];
            //文件路径
            NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"MeasgeHistoryCount.plist"];
            if ([manager removeItemAtPath:filepath error:nil]) {
                NSLog(@"文件删除成功");
                [self updateTableView];
            }
            
            [self presentViewController:[[UINavigationController alloc]initWithRootViewController:[[MsgNotificationViewController alloc]init]] animated:YES completion:nil];
            break;
        }
        case 2:
        {
            
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            SuggestionViewController *loginVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"SuggestionVC"];
            UINavigationController * navLogin = [[UINavigationController alloc] initWithRootViewController:loginVC];
            
            [self presentViewController:navLogin animated:YES completion:nil];
            
            break;
        }
            
            case 3:
        {
            [self presentViewController:[[UINavigationController alloc]initWithRootViewController:[[HelpViewController alloc]init]] animated:YES completion:nil];
        }
            break;
            case 4:
        {
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[ChoosePatternViewController new]] animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}

- (IBAction)loginOut:(UIButton *)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否退出登录?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self signOutInfo];
        [[AppDelegate globalDelegate] setRootViewController];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
